package com.hgy.plugin.rest.helper;

import java.util.ArrayList;
import java.util.List;

import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
import org.apache.cxf.jaxrs.provider.jsonp.JsonpInInterceptor;
import org.apache.cxf.jaxrs.provider.jsonp.JsonpPostStreamInterceptor;
import org.apache.cxf.jaxrs.provider.jsonp.JsonpPreStreamInterceptor;
import org.apache.cxf.message.Message;
import org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.hgy.framework.helper.BeanHelper;
import com.hgy.plugin.rest.RestConfig;

/**
 * REST 助手类
 * 
 * @author alone
 *
 */
public class RestHelper {

	// 日志输出
	private static final List<Interceptor<? extends Message>> inInterceptorList = new ArrayList<Interceptor<? extends Message>>();
	private static final List<Interceptor<? extends Message>> outInterceptorList = new ArrayList<Interceptor<? extends Message>>();
	private static final List<Object> providerList = new ArrayList<Object>();

	static {
		// 添加 JSON Provider
		JacksonJsonProvider jsonProvider = new JacksonJsonProvider();
		providerList.add(jsonProvider);
		if (RestConfig.isLog()) {
			LoggingInInterceptor loggingInInterceptor = new LoggingInInterceptor();
			inInterceptorList.add(loggingInInterceptor);
			LoggingOutInterceptor loggingOutInterceptor = new LoggingOutInterceptor();
			outInterceptorList.add(loggingOutInterceptor);
		}
		// 添加 JSONP Interceptor
		if(RestConfig.isJsonp()){
			JsonpInInterceptor jsonpInInterceptor = new  JsonpInInterceptor();
			jsonpInInterceptor.setCallbackParam(RestConfig.getJsonpFunction());
			inInterceptorList.add(jsonpInInterceptor);
			JsonpPreStreamInterceptor jsonpPreStreamInterceptor = new JsonpPreStreamInterceptor();
			inInterceptorList.add(jsonpPreStreamInterceptor);
			JsonpPostStreamInterceptor jsonpPostStreamInterceptor = new JsonpPostStreamInterceptor();
			inInterceptorList.add(jsonpPostStreamInterceptor);
		}
		// 添加 CORS Provider
		if(RestConfig.isCors()){
			CrossOriginResourceSharingFilter corsProvider = new CrossOriginResourceSharingFilter();
			corsProvider.setAllowOrigins(RestConfig.getCorsOriginList());
			providerList.add(corsProvider);
		}
	}
	
	/**
	 * 发布 REST 服务
	 * 
	 * @param wsdl
	 * @param resourceClass
	 */
	public static void publishService(String wsdl, Class<?> resourceClass){
		
		JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
		factory.setAddress(wsdl);
		factory.setResourceClasses(resourceClass);
		factory.setResourceProvider(new SingletonResourceProvider(BeanHelper.getBean(resourceClass)));
		factory.setProviders(providerList);
		factory.setInInterceptors(inInterceptorList);
		factory.setOutInterceptors(outInterceptorList);
		factory.create();
	}
	
	/**
	 * 创建 REST 客户端
	 * 
	 * @param wsdl
	 * @param resourceClass
	 * @return
	 */
	public static <T> T createClient(String wsdl, Class<? extends T> resourceClass){
		return JAXRSClientFactory.create(wsdl, resourceClass, providerList);
	}
}
